<?php
/**
 * Created by PhpStorm.
 * User: wumengmeng <wu_mengmeng@foxmail.com>
 * Date: 2019/4/26 0026
 * Time: 11:20
 */

namespace HiCommon\Repository;

use HiCommon\MongodbModel\MongoActionAdminLog;
use HiCommon\MongodbModel\MongoLoginAdminLog;
use HiCommon\MongodbModel\MongoPhoneCodeLog;
use HiCommon\MongodbModel\MongoActivityLog;
use HiCommon\MongodbModel\MongoSystemSetLog;
use HiCommon\MongodbModel\MongoGiftLog;
use HiCommon\MongodbModel\MongoJpushLog;
use HiCommon\MongodbModel\MongoMessage;



class LogRepository extends BaseRepository
{

    /**
     * 记录登录日志
     *
     * @param array $arrLog
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function record_login_in(array $arrLog)
    {
       return MongoLoginAdminLog::create($arrLog);
    }

//    /**
//     * 记录操作日志
//     *
//     * @param array $arrLog
//     *
//     * @return mixed
//     * @author wumengmeng <wu_mengmeng@foxmail.com>
//     */
//    public function record_action(array $arrLog)
//    {
//       return MongoActionAdminLog::create($arrLog);
//    }

    /**
     * 获取登录日志
     *
     * @param int $length
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_admin_login_log(int $length = 20)
    {
        return MongoLoginAdminLog::take($length)->orderBy('created_at','desc')->paginate($length);
    }

    /**
     * 获取操作日志
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_admin_action_log_list(int $length = 20)
    {
        return MongoActionAdminLog::orderBy('created_at','desc')->paginate(20);
    }


    /**
     * 记录验证码发送日志
     *
     * @param array $arrLog
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function record_phone_code(array $arrLog)
    {
        $arrLog['send_at'] = time_to_ymdhis();
       return MongoPhoneCodeLog::create($arrLog);
    }


    /**
     * 获取验证码日志
     *
     * @param int $length
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_phone_code_log( string $phone = '' , int $length = 20)
    {   
            return MongoPhoneCodeLog::take($length)->where(
            function ($query)
            use($phone) {
                if(!empty($phone)){
                    $query->where('phone', $phone);
                }
            })->orderBy('created_at','desc')->paginate($length);
    }



    /**
     * 记录定时任务修改活动日志
     * 2019 11 27
     * @param array $arrLog
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function record_change_activity(array $arrLog)
    {
        return MongoActivityLog::create($arrLog);
    }

    /**
     * 获取定时任务修改活动日志
     * 2019 11 27
     * @param int $length
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_change_activity_log(  int $length = 20)
    {
        return MongoActivityLog::take($length)->orderBy('created_at','desc')->paginate($length);
    }


    /**
     * 系统设置信息
     * 2019 11 28
     * @param array $arrLog
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function set_field_info_log(array $arrLog)
    {
        return MongoSystemSetLog::create($arrLog);
    }


    /**
     * 系统设置信息
     * 2019 11 28
     * @param array $arrLog
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function set_field_info_update_log( string $field , array $arrLog)
    {
        return MongoSystemSetLog::where('title', $field)->update($arrLog);
    }

    /**
     * 获取定时任务修改活动日志
     * 2019 11 27
     * @param int $length
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_set_field_info_log(  string $field = '' , int $length = 20  )
    {
        return MongoSystemSetLog::take($length)->orderBy('created_at','desc')->paginate($length);
    }



    /**
     * 记录发放礼包日志
     *
     * @param array $arrLog
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function record_send_gift(array $arrLog)
    {
        return MongoGiftLog::create($arrLog);
    }


    /**
     * 获取发放礼包日志
     *
     * @param int $length
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_send_gift_log( string $phone = '' , int $length = 20)
    {
        return MongoGiftLog::take($length)->where(
            function ($query)
            use($phone) {
                if(!empty($phone)){
                    $query->where('phone', $phone);
                }
            })->orderBy('created_at','desc')->paginate($length);
    }





    /**
     * 添加极光推送消息日志记录
     *
     * @param array $arrLog
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function record_send_jpush(array $arrLog)
    {
        return MongoJpushLog::create($arrLog);
    }


    /**
     * 获取极光推送消息日志列表
     *
     * @param array $arrLog
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_record_jpush(array $arr_input = [] ,  $length = 20 )
    {
        return MongoJpushLog::take($length)->where(
            function ($query)
            use($arr_input) {
                if(!empty($arr_input['status'])){
                    $query->where('status', $arr_input['status']);
                }
            })->orderBy('created_at','desc')->paginate($length);
    }

    /**
     *  消息类型统计
     *  hinq 2020 5 18
     *
     **/
    public function count_where( array $arr_where ){
        return MongoMessage::where($arr_where)->count();
    }


    /**
     * 添加系统消息日志记录
     *
     * @param array $arrLog
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function message_add(array $arrLog)
    {
        return MongoMessage::create($arrLog);
    }


    /**
     * 获取系统消息日志记录
     *
     * @param array $arrLog
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_message_list(array $arr_input = [] ,  $length = 20 )
    {
        return MongoMessage::take($length)->where(
            function ($query)
            use($arr_input) {
                if(!empty($arr_input['phone'])){
                    $query->where('user_id', $arr_input['phone']);
                }
                if(!empty($arr_input['user_id'])){
                    $query->where('user_id', $arr_input['user_id']);
                }
                if(!empty($arr_input['pid'])){
                    $query->where('pid', (int) $arr_input['pid']);
                }
            })->orderBy('created_at','desc')->paginate($length);
    }


    /**
     *  修改指定数据想阅读状态
     *
     * @param array $arrLog
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function update_message(array $where = [] ,  $arr_input = [] )
    {
        return MongoMessage::where($where)->update($arr_input);
    }


    public function update_message_whereIn(array $ids = [] ,  $arr_input = [] )
    {
        return MongoMessage::where( '_id' , $ids )->update($arr_input);
    }

    //删除指定数据
    public function del_message()
    {
        return MongoMessage::where('_id','5eba47b0c347a71c580046a6')->delete();
    }




    public function del()
    {
        return MongoGiftLog::where('_id','5e5726f7c347a72ea8001822')->delete();
    }

}